package com.pajk.user.jmeter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pajk.user.api.UserService;
import com.pajk.user.model.User;

public class UserService_getUserById extends AbstractJavaSamplerClient {

	private String jMeterPrefix = null;
	private String jMeterOutput = null;
	private int jMeterRepeat = 0;

	private ClassPathXmlApplicationContext appContext = null;

	private UserService userService = null;

	int groupId = 0;
	int threadId = 0;

	String[] userIds = null;

	private boolean sanity = false;

	public static void main(String[] args) {

		Thread.currentThread().setName("Thread Group 1-1");
		JavaSamplerContext context = new JavaSamplerContext(new Arguments());
		UserService_getUserById test = new UserService_getUserById();
		test.setupTest(context);
		test.runTest(context);
		test.teardownTest(context);
	}

	public Arguments getDefaultParameters() {

		Arguments params = new Arguments();

		params.addArgument(Utility.JMETER_PREFIX_LABEL,
				Utility.JMETER_PREFIX_DEFAULT);
		params.addArgument(Utility.JMETER_OUTPUT_LABEL,
				Utility.JMETER_OUTPUT_DEFAULT);
		params.addArgument(Utility.JMETER_REPEAT_LABEL, new Integer(
				Utility.JMETER_REPEAT_DEFAULT).toString());

		return params;
	}

	public void setupTest(JavaSamplerContext context) {

		super.setupTest(context);

		jMeterPrefix = context.getParameter(Utility.JMETER_PREFIX_LABEL,
				Utility.JMETER_PREFIX_DEFAULT);
		jMeterOutput = context.getParameter(Utility.JMETER_OUTPUT_LABEL,
				Utility.JMETER_OUTPUT_DEFAULT);
		jMeterRepeat = context.getIntParameter(Utility.JMETER_REPEAT_LABEL,
				Utility.JMETER_REPEAT_DEFAULT);

		appContext = new ClassPathXmlApplicationContext(
				Utility.USER_SERVICE_REFERENCE_DEFINITION);
		appContext.start();

		userService = (UserService) appContext.getBean("userService");

		String name = Thread.currentThread().getName();
		int GroupId = Utility.parseGroupId(name);
		int ThreadId = Utility.parseThreadId(name);

		try {
			userIds = Utility.populateUserIdsCacheFromLog(jMeterPrefix,
					jMeterOutput, jMeterRepeat, GroupId, ThreadId);
		} catch (FileNotFoundException e) {
			this.getLogger().error(e.getMessage(), e.getCause());
		} catch (IOException e) {
			this.getLogger().error(e.getMessage(), e.getCause());
		}

		sanity = true;
	}

	@Override
	public SampleResult runTest(JavaSamplerContext arg0) {

		SampleResult sampleResult = new SampleResult();

		sampleResult.setDataEncoding(Utility.ENCODING);

		int count = new Random().nextInt(jMeterRepeat);
		long userId = Long.parseLong(userIds[count]);

		try {
			sampleResult.sampleStart();

			User user = userService.getUserById(userId);

			if (sanity) {
				verify(user);
				sanity = false;
			}

			sampleResult.setSuccessful(true);
		} catch (Exception e) {
			sampleResult.setSuccessful(false);
			this.getLogger().error(e.getMessage(), e.getCause());
		} finally {
			if (sampleResult != null) {
				sampleResult.sampleEnd();
			}
		}

		return sampleResult;
	}

	public void teardownTest(JavaSamplerContext context) {

		super.teardownTest(context);

		appContext.close();
	}

	private void verify(User user) throws Exception {

		if (user != null) {
			System.out.println("user.id: " + user.getId());
		} else {
			System.out.println("user is null");
		}
	}

}